package com.sgcc.system.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.system.domain.Jjrgl;
import com.sgcc.system.service.IJjrglService;

import io.swagger.annotations.ApiOperation;

import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 节假日管理Controller
 * 
 * @author sgcc
 * @date 2021-03-29
 */
@RestController
@RequestMapping("/jjrgl")
public class JjrglController extends BaseController
{
    @Autowired
    private IJjrglService jjrglService;

    /**
     * 查询节假日管理列表
     */
//    //@PreAuthorize(hasPermi = "system:jjrgl:list")
    @GetMapping("/list")
    public TableDataInfo list(Jjrgl jjrgl)
    {
        startPage();
        List<Jjrgl> list = jjrglService.selectJjrglList(jjrgl);
        return getDataTable(list);
    }

    /**
     * 导出节假日管理列表
     */
//    //@PreAuthorize(hasPermi = "system:jjrgl:export")
    @Log(title = "节假日管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Jjrgl jjrgl) throws IOException
    {
        List<Jjrgl> list = jjrglService.selectJjrglList(jjrgl);
        ExcelUtil<Jjrgl> util = new ExcelUtil<Jjrgl>(Jjrgl.class);
        util.exportExcel(response, list, "jjrgl");
    }
//    //@PreAuthorize(hasPermi = "system:jjrgl:initialization")
    @Log(title = "初始化节假日管理", businessType = BusinessType.INSERT)
    @PostMapping("/initialization")
    public AjaxResult initialization() throws IOException, ParseException
    {
		jjrglService.deleteJjrglAll();
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	SimpleDateFormat year = new SimpleDateFormat("yyyy");
    		Date now=new Date();
    		Jjrgl wy=new Jjrgl();
        	wy.setLx("01");//节日
        	wy.setJqmc("五一");
        	wy.setKssj(sdf.parse(year.format(now)+"-05-01"));
        	wy.setJssj(sdf.parse(year.format(now)+"-05-05"));
        	jjrglService.insertJjrgl(wy);
    	
    	

    		Jjrgl yd=new Jjrgl();
        	yd.setLx("01");//节日
        	yd.setJqmc("元旦");
        	yd.setKssj(sdf.parse(year.format(now)+"-01-01"));
        	yd.setJssj(sdf.parse(year.format(now)+"-01-03"));
        	jjrglService.insertJjrgl(yd);
		
    	
    	
  
    		Jjrgl gq=new Jjrgl();
        	gq.setLx("01");//节日
        	gq.setJqmc("国庆");
        	gq.setKssj(sdf.parse(year.format(now)+"-10-01"));
        	gq.setJssj(sdf.parse(year.format(now)+"-10-07"));
        	jjrglService.insertJjrgl(gq);
		
    	
    	List<String> week=getWeekends(Integer.valueOf(year.format(now)));
    	for (String str : week) {
    		Jjrgl sx=new Jjrgl();
    		sx.setLx("02");//双休
    		sx.setJqmc("双休");
    		sx.setKssj(sdf.parse(str));
    		sx.setJssj(sdf.parse(str));
        	jjrglService.insertJjrgl(sx);
		}
        return AjaxResult.success("初始化成功！");
    }
    public static List<String> findDates(String dBegin,String dEnd){
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd") ;
    	try {
	    	Date dE = simpleDateFormat.parse (dEnd) ;
	    	Date db = simpleDateFormat.parse (dBegin) ;
	    	List<String> lDate = new ArrayList<String>();
	    	lDate.add (dBegin) ;
	    	Calendar calBegin = Calendar.getInstance();calBegin.setTime (db) ;
	    	Calendar calEnd = Calendar.getInstance();calEnd.setTime (dE) ;
	    	while (dE.after (calBegin.getTime ())) {
		    	calBegin.add (Calendar.DAY_OF_MONTH, 1);
		    	lDate.add (simpleDateFormat.format (calBegin.getTime ( ) ) );
	    	}
	    	return lDate;
    	}catch(ParseException e){
    		e.printStackTrace () ;
    	return null;
    	}
    }

    public static List<String> getWeekends(int year){
    	List<String> list = new ArrayList<String>();
    	final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	Calendar cal = Calendar.getInstance(Locale.CHINA);
    	cal.set(year, 0, 1);
    	for(int day = 1; day <= cal.getActualMaximum(Calendar.DAY_OF_YEAR); day++){
	    	cal.set(Calendar.DAY_OF_YEAR, day);
	    	int weekDay = cal.get(Calendar.DAY_OF_WEEK);
	    	if(weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY){
	    		list.add(sdf.format(cal.getTime()));
	    	}
    	}
    	return list;
    }

    
    @ApiOperation("法定节假日列表-日历")
	@GetMapping("/fdjjrList")
    public List<Map<String, Object>> fdjjrList() {
    	List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	//保存日期集合
//    	List<String> dateList = new ArrayList<String>();
    	List<Jjrgl> list = jjrglService.selectJjrglList(null);
		for (int i = 0; i < list.size(); i++) {
			Jjrgl fdjjr = list.get(i);
			Date date_start = fdjjr.getKssj();
			Date date_end = fdjjr.getJssj();
			Date date = date_start;
			//用Calendar 进行日期比较判断
			Calendar cd = Calendar.getInstance();
			while (date.getTime() <= date_end.getTime()) {
				cd.setTime(date);
				Map<String, Object> map = new HashMap<>();
				map.put(sdf.format(date), fdjjr);
				resultList.add(map);
				//增加一天 放入集合
				cd.add(Calendar.DATE, 1);
				date = cd.getTime();
			};
		}
    	return resultList;
    }
    /**
     * 获取节假日管理详细信息
     */
//    //@PreAuthorize(hasPermi = "system:jjrgl:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jjrglService.selectJjrglById(id));
    }

    /**
     * 新增节假日管理
     */
//    //@PreAuthorize(hasPermi = "system:jjrgl:add")
    @Log(title = "节假日管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Jjrgl jjrgl)
    {
        return AjaxResult.success(jjrglService.insertJjrgl(jjrgl));
    }

    /**
     * 修改节假日管理
     */
//    //@PreAuthorize(hasPermi = "system:jjrgl:edit")
    @Log(title = "节假日管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Jjrgl jjrgl)
    {
        return toAjax(jjrglService.updateJjrgl(jjrgl));
    }

    /**
     * 删除节假日管理
     */
//    //@PreAuthorize(hasPermi = "system:jjrgl:remove")
    @Log(title = "节假日管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jjrglService.deleteJjrglByIds(ids));
    }
    
    @PostMapping("/findIsJjr")
    public TableDataInfo findIsJjr(@RequestParam("date") String date)
    {	
    	
    	List<Jjrgl> list=jjrglService.findIsJjr(date);
        return getDataTable(list);
    }
}
